package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.content.TbContentCategory;
import com.dongdongshop.service.ContentCategoryServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentCategory")
public class ContentCategoryController {

    private final Logger logger= LoggerFactory.getLogger(ContentCategoryController.class);

    @Reference
    private ContentCategoryServiceI contentCategoryServiceI;

    @RequestMapping("showContentCategory")
    public String showContentCategory(){
        return "admin/content_category";
    }

    @RequestMapping("listCategory")
    @ResponseBody
    public Result<List<TbContentCategory>> listCategory(){
        try {
            List<TbContentCategory> listCategory =contentCategoryServiceI.listCategory();
            return Result.jsonResponse(HttpResponseEnum.OK).setData(listCategory);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("insertCategory")
    @ResponseBody
    public Result insertCategory(TbContentCategory contentCategory){
        try {
            contentCategoryServiceI.insertCategory(contentCategory);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("getCategory/{id}")
    @ResponseBody
    public  Result<TbContentCategory> getCategory(@PathVariable("id")Long id){
        try {
            TbContentCategory contentCategory=contentCategoryServiceI.getCategory(id);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(contentCategory);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("updateCategory")
    @ResponseBody
    public Result updateCategory(TbContentCategory contentCategory){
        try {
            contentCategoryServiceI.updateCategory(contentCategory);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }

    }

    @RequestMapping("deleteCategory")
    @ResponseBody
    public Result deleteCategory(@RequestParam("ids[]")Long[] ids){
        try {
            contentCategoryServiceI.deleteCategory(ids);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
}
